<?php

/**
 * 为了解决mongodb导入csv文件时，出现错误的问题
 * 主要是
 */

//检测字段串是不是utf8
//
define('INFILE', 'test.csv');
define('OUTFILE', 'outfile.csv');
define('BADFILE', 'bad.csv');

$patt_check = '/^(.*?)\s?#\s?(.*?)\s?#\s?(.*?)$/';
$patt_format = '%s,%s,%s';




function mb_is_utf8($string)   
{   
    return mb_detect_encoding($string, 'UTF-8') === 'UTF-8';//新发现   
}  

if (!file_exists(INFILE)) die('目标文件不存在!');
if (file_exists(OUTFILE)) unlink(OUTFILE);
if (file_exists(OUTFILE)) unlink(OUTFILE);

$file = fopen(INFILE, 'r') or die('打开文件错误');
//一次读取1M文件
$lastline = '';
$badcount = 0;
$goodcount = 0;
$bf = fopen(BADFILE, 'a+');
$gf = fopen(OUTFILE, 'a+');
while (!feof($file)) {
	$line = fgets($file, 1024);
	$list  = array();
	$badstr = '';$goodstr = '';
	$list = explode("\r\n", $line);
	$count = count($list);
	
	$list[0] = $lastline.$list[0];  //处理第一行
	$ccount = $count -1;
	for ($i = 0; $i<$ccount; $i++){  //处理中间行
		if (!mb_is_utf8($list[$i])) {
			$badstr = $badstr  +'\r\n'+  $list[$i];
			$badcount++;
		} else {
			$result = preg_match($patt_check, $list[$i], $matchs);
			if ($result) {
				$goodstr = $goodstr +'\r\n'+ sprintf($patt_format, $matchs[1],$matchs[2],$matchs[3]);
				$goodcount++;
			} else {
				$badstr = $badstr  +'\r\n'+  $list[$i];
				$badcount++;
			}
		}
	}
	$lastline = $list[$ccount];    //处理最后一行
	//写入文件
	
	fwrite($gf,$goodstr);
	fwrite($gf,$badstr);
}
fclose($gf);
fclose($bf);
echo "$goodcount 条正确记录!<br>";
echo "$badcount 条无效记录!<br>";


?>